package org.apache.tomcat.util.buf;

/**
 * UriUtil
 *
 * @author chentudong
 * @date 2024/2/19 16:01
 * @since 1.0
 */
public class UriUtil {

  public static boolean isAbsoluteURI(String path) {
    if (path.startsWith("file:/")) {
      return true;
    }

    int i = 0;
    while (i < path.length() && isSchemeChar(path.charAt(i))) {
      i++;
    }

    if (i == 0) {
      return false;
    }

    return i + 2 < path.length() && path.charAt(i++) == ':' && path.charAt(i++) == '/'
        && path.charAt(i) == '/';
  }

  /**
   * Determine if the character is allowed in the scheme of a URI.
   * See RFC 2396, Section 3.1
   *
   * @param c The character to test
   *
   * @return {@code true} if a the character is allowed, otherwise {code
   *         @false}
   */
  private static boolean isSchemeChar(char c) {
    return Character.isLetterOrDigit(c) || c == '+' || c == '-' || c == '.';
  }
}
